package com.amir.common.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.widget.ImageView;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;


import java.security.MessageDigest;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;


/**
 * @author justkeepmoving.
 * @date 2019/6/12 15:33.
 * make your program better than last time.
 */
public class ImageHelper {

    public static void displayImage(Context context, String url, ImageView imgThumb) {
        GlideApp.with(context)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .into(imgThumb);
    }

    public static void displayImage(FragmentActivity fragment, String url, ImageView imgThumb) {
        GlideApp.with(fragment)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .into(imgThumb);
    }

    public static void displayImageNoAnim(Activity activity, String url, ImageView imgThumb) {
        GlideApp.with(activity)
                .load(url)
                .placeholder(imgThumb.getDrawable())
                .into(imgThumb);
    }

    public static void displayImage(Activity fragmentActivity, String url, ImageView imgThumb) {
        GlideApp.with(fragmentActivity)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .into(imgThumb);
    }

    public static void displayRoundImage(Context context, String url, ImageView imgThumb) {
        GlideApp.with(context)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .transform(new GlideRoundTransform())
                .into(imgThumb);
    }

    public static void displayRoundImage(Activity fragmentActivity, String url, ImageView imgThumb) {
        GlideApp.with(fragmentActivity)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .transform(new GlideRoundTransform())
                .into(imgThumb);
    }

    public static void displayRoundImageNoAnim(Activity activity, String url, ImageView imgThumb) {
        GlideApp.with(activity)
                .load(url)
                .placeholder(imgThumb.getDrawable())
                .transform(new GlideRoundTransform())
                .into(imgThumb);
    }

    public static class GlideRoundTransform extends BitmapTransformation {

        private float radius = 0f;

        public GlideRoundTransform() {
            this(5);
        }

        public GlideRoundTransform(int dp) {
            this.radius = Resources.getSystem().getDisplayMetrics().density * dp + 0.5f;
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            Bitmap bitmap = TransformationUtils.centerInside(pool, toTransform, outWidth, outHeight);
            return roundCrop(pool, bitmap);
        }

        private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) {
                return null;
            }
            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
        }

        public String getId() {
            return getClass().getName() + Math.round(radius);
        }

        @Override
        public void updateDiskCacheKey(MessageDigest messageDigest) {

        }
    }

    public static Bitmap getRoundBitmap(Bitmap source) {
        int width = source.getWidth();
        Bitmap result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, width, source.getHeight());
        canvas.drawRoundRect(rectF, width / 2, width / 2, paint);
        return result;
    }

    public static void displayRoundImage(FragmentActivity fragmentActivity, String url, ImageView imgThumb, int radius) {
        GlideApp.with(fragmentActivity)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .transform(new GlideRoundTransform(radius))
                .into(imgThumb);
    }

    public static void displayImage(Fragment fragment, String url, ImageView imgThumb) {
        GlideApp.with(fragment)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .into(imgThumb);
    }

    public static void displayCircleImage(FragmentActivity fragmentActivity, String url, ImageView imgThumb) {

        GlideApp.with(fragmentActivity)
                .load(url)
                .circleCrop()
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .into(imgThumb);
    }

    public static void displayCircleImage(Fragment fragment, String url, ImageView imgThumb) {
        GlideApp.with(fragment)
                .load(url)
                .circleCrop()
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .into(imgThumb);
    }

    public static void displayCircleImage(Context context, String url, ImageView imgThumb) {
        GlideApp.with(context)
                .load(url)
                .circleCrop()
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .into(imgThumb);
    }

    public static void displayCircleImage(Activity activity, String url, ImageView imgThumb) {
        GlideApp.with(activity)
                .load(url)
                .circleCrop()
                .transition(DrawableTransitionOptions.withCrossFade(200))
                .into(imgThumb);
    }

    public static void displayCircleImageNoAnim(Activity activity, String url, ImageView imgThumb) {
        GlideApp.with(activity)
                .load(url)
                .circleCrop()
                .into(imgThumb);
    }

}
